package com.logic.modular.game.robot.po;

import com.logic.comment.util.RedisUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

import static com.logic.modular.game.GameConstant.ROBOT_PLAYER_INFO_KEY;

/**
 * 人机行为信息
 */
@SuppressWarnings("ALL")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RobotInfo {
    private static final Logger log = LoggerFactory.getLogger(RobotInfo.class);
    private Long robotId;
    /**
     * 匹配id -1 处于匹配中
     */
    private Long matchId;
    /**
     * 0.未进行任何操作
     */
    private Integer status;
    /**
     * 未配队
     */
    public Set<Integer> teamsStatus0;
    /**
     * 已配队
     */
    public Set<Integer> teamsStatus1;
    /**
     * 移动/战斗
     */
    public Set<Integer> teamsStatus2;
    /**
     * 上次攻击时间
     */
    public Long lastAttackTime;


    public RobotInfo(Long robotId) {
        this.robotId = robotId;
        this.status = 0;
        teamsStatus0 = new HashSet<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
            add(5);
        }};
        teamsStatus1 = new HashSet<>();
        teamsStatus2 = new HashSet<>();
        lastAttackTime = System.currentTimeMillis();
    }

    public static List<RobotInfo> list() {
        return RedisUtils.redisUtils().hGetAll(ROBOT_PLAYER_INFO_KEY).values().stream().map(item -> (RobotInfo) item).collect(Collectors.toList());
    }

    public static RobotInfo get(Long robotId) {
        return (RobotInfo) RedisUtils.redisUtils().hGet(ROBOT_PLAYER_INFO_KEY, robotId.toString());
    }

    public static void remove(Long matchId) {
        Object[] array = list().stream()
                .filter(item -> item.getMatchId().equals(matchId))
                .map(item -> item.getRobotId().toString())
                .collect(Collectors.toSet())
                .toArray();
        log.info("移除机器人:{}", array);
        if (array.length != 0) {
             RedisUtils.redisUtils().hDelete(ROBOT_PLAYER_INFO_KEY, array);
            // 移除攻击
            removeNeedRobotTeamsAttackMatchId(matchId);
        }
    }

    public void save() {
        RedisUtils.redisUtils().hPut(ROBOT_PLAYER_INFO_KEY, this.getRobotId().toString(), this);
    }

    /**
     * 获取未配队的队伍编号
     * @param matchId 匹配id
     * @return MapUtil<Long, Set<Integer>>
     */
    public static Map<Long, Set<Integer>> obtainNoFormationTeamsNo(Long matchId) {
        return list().stream()
                .filter(item -> !item.getTeamsStatus0().isEmpty() && item.getMatchId().equals(matchId))
                .collect(Collectors.toMap(RobotInfo::getRobotId, RobotInfo::getTeamsStatus0));
    }

    /**
     * 获取配队且未在战斗中的机器人的队伍编号
     * @param matchId 匹配id
     * @return MapUtil<Long, Set<Integer>>
     */
    public static Map<Long, Set<Integer>> obtainNoBattleTeamsNo(Long matchId) {
        return list().stream()
                .filter(item -> !item.getTeamsStatus1().isEmpty() && item.getMatchId().equals(matchId))
                .collect(Collectors.toMap(RobotInfo::getRobotId, RobotInfo::getTeamsStatus1));
    }


    /**
     * 保存需要人机队伍攻击的匹配
     * @param matchId
     */
    public static void saveNeedRobotTeamsAttackMatchId(Long matchId){
        String key = "needRobotTeamsAttackMatchId";
        Set<Long> robotTeamsAttackMatchId = (Set<Long>) RedisUtils.redisUtils().get(key);
        if (robotTeamsAttackMatchId == null){
            robotTeamsAttackMatchId = new HashSet<>();
        }
        robotTeamsAttackMatchId.add(matchId);
        RedisUtils.redisUtils().set(key, robotTeamsAttackMatchId);
    }
    public static void removeNeedRobotTeamsAttackMatchId(Long matchId){
        String key = "needRobotTeamsAttackMatchId";
        Set<Long> robotTeamsAttackMatchId = (Set<Long>) RedisUtils.redisUtils().get(key);
        if (robotTeamsAttackMatchId == null){
            robotTeamsAttackMatchId = new HashSet<>();
        }
        robotTeamsAttackMatchId.remove(matchId);
        RedisUtils.redisUtils().set(key, robotTeamsAttackMatchId);
    }
    public static Set<Long> obtainNeedRobotTeamsAttackMatchId(){
        String key = "needRobotTeamsAttackMatchId";
        Set<Long> robotTeamsAttackMatchId = (Set<Long>) RedisUtils.redisUtils().get(key);
        if (robotTeamsAttackMatchId == null){
            robotTeamsAttackMatchId = new HashSet<>();
        }
        return robotTeamsAttackMatchId;
    }
}